package com.example.bms.user.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.bms.user.entity.StaffinfoEntity;
import com.example.bms.user.entity.UserEntity;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;

import com.example.bms.user.dao.UserinfoDao;
import com.example.bms.user.entity.UserinfoEntity;
import com.example.bms.user.service.UserinfoService;
import org.springframework.util.ObjectUtils;



@Service("userinfoService")
public class UserinfoServiceImpl extends ServiceImpl<UserinfoDao, UserinfoEntity> implements UserinfoService {

    @Autowired
    private UserinfoDao userinfoDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserinfoEntity> page = this.page(
                new Query<UserinfoEntity>().getPage(params),
                new QueryWrapper<UserinfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public String[] login(Map<String, Object> params) {

        String wxId =(String) params.get("wxId");

        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        String APP_ID="wx3035b89506c4ec27";
        String SECRET="73c7194810bb3e61efdc0c94b391f6bc";

        String url= new StringBuilder("https://api.weixin.qq.com/sns/jscode2session?appid=").append(APP_ID)
                .append("&secret=").append(SECRET)
                .append("&js_code=").append(wxId).append("&grant_type=authorization_code").toString();

        // 创建Post请求
        HttpPost httpPost = new HttpPost(url);

        httpPost.setHeader("Content-Type", "application/json;charset=utf8");

        // 响应模型
        CloseableHttpResponse response = null;
        String info = null;
        try {
            // 由客户端执行(发送)Post请求
            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();

            if (responseEntity != null) {

                info = EntityUtils.toString(responseEntity);

            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        JSONObject jsonObject1 = JSONObject.parseObject(info);
        String openid = jsonObject1.getString("openid");

        QueryWrapper<UserinfoEntity> queryWrapper=new QueryWrapper<UserinfoEntity>();

        if(!ObjectUtils.isEmpty(openid)){
            queryWrapper.eq("openid",openid);
        }

        IPage<UserinfoEntity> page = this.page(
                new Query<UserinfoEntity>().getPage(params),
                queryWrapper
        );

        List list=new PageUtils(page).getList();

        int size=list.size();

        String sizes=String.valueOf(size);

        String[] ids={openid,sizes};

        return ids;
    }

    @Override
    public Integer insert(Map<String, Object> params) {
        UserinfoEntity userinfo=new UserinfoEntity();
        String openid =(String) params.get("openid");
        userinfo.setOpenid(openid);
        userinfoDao.insertuserinfo(userinfo);
        int id=userinfo.getUid();
        return id;
    }

    @Override
    public List search(Map<String, Object> params) {
        String openid =(String) params.get("openid");
        List a=userinfoDao.selectuservo(openid);
        return a;
    }

}